﻿using System;
using Core.CSV;
using Core.Utils;
using UnityEngine;

namespace Game.View {
    //效果槽
    public class WeaponEffectSlot : MonoBehaviour {
        public int Index {
            get;
            private set; 
        }
        public WeaponEffectItem Item {
            get {
                return mItem;
            }
        }
        public TBWeaponEffectConfig Data {
            get {
                return mItem == null ? null : mItem.Data;
            }
        }
        public Action<WeaponEffectSlot> OnItemChanged;
        
        private WeaponEffectItem mItem;
        public void Refresh(int index,uint effectID) {
            Index = index;
            if (mItem) {
                PoolMgr.Ins.Release(mItem.gameObject);
                mItem = null;
            }
            if (effectID == default) {
                return;
            }
            mItem = PoolMgr.Ins.Spawn("WeaponEffectItem").GetComponent<WeaponEffectItem>();
            mItem.transform.SetParent(transform);
            mItem.Init(effectID);
        }

        public void UpdateItem(WeaponEffectItem item) {
            mItem = item;
            if (item) {
                item.transform.SetParent(transform);
                item.transform.localPosition = Vector3.zero;   
            }
            OnItemChanged?.Invoke(this);
        }
    }
}